//
//  XYTabBar.swift
//  SinaWB_XY
//
//  Created by 小熊猫 on 15/10/9.
//  Copyright © 2015年 小熊猫. All rights reserved.
//

import UIKit

class XYTabBar: UITabBar {

    var plusButtonBlock:(()->())?
    var plusButton:UIButton?
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let btn = UIButton()
        
        //添加点击事件
        btn.addTarget(self, action: "didPlusButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        //给中间按钮设置图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
       
        btn.sizeToFit()
        self.addSubview(btn)
        self.plusButton=btn
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //设置tabbar上的位置
    override func layoutSubviews() {
        super.layoutSubviews()
        
        plusButton!.center=CGPointMake(self.center.x, self.bounds.height*0.5)
        
        var index=0
        
        let w=self.bounds.width/5
        
        for view in self.subviews{
            if view .isKindOfClass(NSClassFromString("UITabBarButton")!){
                view.frame=CGRectMake(w*CGFloat(index), 0, w, self.bounds.height)
                index++
                if index==2{
                    index++
                }
            }
        }
        
    }
    
//中间按钮的点击事件
    @objc private func didPlusButton(){
        printLog("中间按钮的点击事件")
   
        if let block=self.plusButtonBlock{
            block()
        }
    }
    

    


}




